
//长庆， biaoduanid:-1          中心点：108.225105，34.995457
//中缅， biaoduanid:77,80,83    中心点： 99.75586，25.48828
//中俄， biaoduanid:6903,6926   中心点：126.03516，47.37305




//各管段的各标段原始数据存储变量
var arrCQ = new Array();
var arrZM = new Array();
var arrZM1 = new Array();
var arrZM2 = new Array();

var arrZE = new Array();
var arrZE1 = new Array();


//初始化数据函数
function initData() {

    // var compare  =function(itemA,itemB){
    // }

    for (index in jsonData) {
        // biaoduanid: -1
        // fjd: 108.6967333
        // flc: 192009.386
        // fwd: 34.54586716
        // iid: "01050280000000000000000000004BB7"
        // name: "管线 + 192009.386"
        // oid: null
        // projectname: "管线-1"
        var item = jsonData[index];

        switch (item.biaoduanid) {
            case -1:
                arrCQ.push(item);
                break;
            case 77:
                arrZM.push(item);
                break
            case 80:
                arrZM1.push(item);
                break
            case 83:
                arrZM2.push(item);
                break
            case 6903:
                arrZE.push(item);
                break;
            case 6926:
                arrZE1.push(item);
                break;
            default:
                break
        }
    }
}


//中心点
var center = [108.225105, 34.995457];
// var centerLonLat = ol.proj.fromLonLat(center);
var projection = ol.proj.get("EPSG:4326");
//切片地图图层
var layer = new ol.layer.Tile({
    source: new ol.source.XYZ({
        url: 'http://192.168.110.221:8080/geoserver/workspaces/workspace/maps/mapName/zxyTileImage/{z}/{x}/{y}.png'
    })
});


//地图视图
var view = new ol.View({
    zoom: 7,
    center: center,
    projection
});

//地图
var map = new ol.Map({
    target: 'map',
    layers: [layer],
    view: view,

    interactions: ol.interaction.defaults({
        altShiftDragRotate: false,
        pinchRotate: false
    }),

    controls: ol.control.defaults({
        attribution: false,
        zoom: false,
        rotate: false
    }).extend([
        //new ol.control.LayerSwitcher({ trash: false, extent: true }),
        //new ol.control.ScaleLine(),
        //new ol.control.Zoom({ target: undefined}),
        //new ol.control.Rotate({
        // autoHide: true,
        // label: “N”
        //}),
        //new ol.control.MeasureTool({ sphereradius: 6378137 })
    ])
});










//排序
// var compare = function (itemA, itemB) {
//   var flcA = item.flc;
//   var flcB = item.flc;
//   if (flcA < flcB) {
//     return -1;
//   } else if (flcA > flcB) {
//     return 1;
//   } else {
//     return 0;
//   }
// }

// arrCQ.sort(compare);




//获取样式
var styleFunction = function (feature) {
    var geometry = feature.getGeometry();
    //线段样式
    var styles = [
        new ol.style.Style({
            fill: new ol.style.Fill({
                color: '#0044CC'
            }),
            stroke: new ol.style.Stroke({
                // lineDash: [1, 3, 5],
                width: 2,
                color: [255, 0, 0, 1]
            })
        })
    ];
    //箭头样式
    geometry.forEachSegment(function (start, end) {
        var arrowLonLat = [(end[0] + start[0]) / 2, (end[1] + start[1]) / 2];
        var dx = end[0] - start[0];
        var dy = end[1] - start[1];
        var rotation = Math.atan2(dy, dx);
        styles.push(new ol.style.Style({
            geometry: new ol.geom.Point(arrowLonLat),
            image: new ol.style.Icon({
                src: "img/arrow.png",
                anchor: [0.75, 0.5],
                rotateWithView: true,
                rotation: -rotation,
                scale: 0.3
            })
        }));
    });
    return styles;
};



//描点Layer
// var arcLayer = new ol.layer.Vector({
//     source: new ol.source.Vector()
// });


// //线
// var geometry = new ol.geom.LineString([]);
// for (index in arrCQ) {
//     var lngLat = [arrCQ[index].fjd, arrCQ[index].fwd];
//     geometry.appendCoordinate(lngLat);
//     var anchor = new ol.Feature({
//         geometry: new ol.geom.Point(lngLat)
//     });
//     // 设置样式，在样式中就可以设置图标
//     anchor.setStyle(new ol.style.Style({
//         image: new ol.style.Circle({
//             radius: 3,
//             stroke: new ol.style.Stroke({
//                 color: '#fff'
//             }),
//             fill: new ol.style.Fill({
//                 color: '#3399CC'
//             })
//         })
//     }));
//     arcLayer.getSource().addFeature(anchor);
// }



//map.addLayer(arcLayer);



//初始化地图涂层
function initLayer(pipeArray) {

    var lineSource = new ol.source.Vector();

    pipeArray.forEach((item) => {

        var geometry = new ol.geom.LineString([]);
        for (index in item) {
            var lngLat = [item[index].fjd, item[index].fwd];
            geometry.appendCoordinate(lngLat);

        }
        var feature = new ol.Feature({
            geometry: geometry
        });
        lineSource.addFeature(feature);
    });
    //标记点集
    var vector = new ol.layer.Vector({
        source: lineSource,
        style: styleFunction
    });

    return vector;
}


//视图跳转方法
function zoomTo(pipeName) {
    var zCenter = [126.03516, 47.37305];
    switch (pipeName) {
        case "zm":
            zCenter = [99.75586, 25.48828];
            break;
        case "ze":
            zCenter = [126.03516, 47.37305];
            break;
        case "qx":
            zCenter = [108.225105, 34.995457];
            break;

        default:
            break;
    }
    view.animate({
        center: zCenter,
        duration: 3000,
    });
}
//调用初始化json数据
initData();

// var compare = function (itemA, itemB) {
//     var flcA = itemA.flc;
//     var flcB = itemB.flc;
//     if (flcA < flcB) {
//         return -1;
//     } else if (flcA > flcB) {
//         return 1;
//     } else {
//         return 0;
//     }
// }


// arrCQ.sort(compare);
// arrZM.sort(compare);
// arrZE.sort(compare);
//获取管段数据


var zePipe = [arrZE, arrZE1];
var zmPipe = [arrZM, arrZM1, arrZM2];
var qxPipe = [arrCQ];

var linelayer = initLayer(zePipe);
var zeLayer = initLayer(zmPipe);
var zmLayer = initLayer(qxPipe);
map.addLayer(linelayer);
map.addLayer(zeLayer);
map.addLayer(zmLayer);



function zeClick() {
    zoomTo("ze");
}
function zmClick() {
    zoomTo("zm");
}
function qxClick() {
    zoomTo("qx");
}


